/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package ui;

import core.DatabaseTable;
import core.TableColumn;
import java.awt.Dimension;
import java.awt.event.ItemListener;
import javax.swing.DefaultListModel;
import javax.swing.JList;
import javax.swing.JOptionPane;
import javax.swing.ListSelectionModel;
import org.apache.log4j.Logger;

import ui.helpers.EditTableListener;

/**
 *
 * @author Sakis Vtdk <sakis.vtdk@gmail.com>
 */
public class TableActionsPanel extends javax.swing.JPanel {

	/**
	 * Creates new form TableActionsPanel
	 */
	public TableActionsPanel() {
		initComponents();
		//TODO: Fix it 
		setComponentsWidth();
	}

	public TableActionsPanel(DatabaseTable dbTable)
	{
		initComponents();

		nameTextField.setText(dbTable.getName());
		
		columnsList.setModel(createListModel(dbTable)); 

		this._dbTable = dbTable;
		//TODO: Fix it 
		//setComponentsWidth();
	}

	private void setComponentsWidth()
	{
		//we want it 15 columns
		//nameTextField.setColumns(this.COMPONENT_WIDTH);	
		nameTextField.setPreferredSize(new Dimension(this.COMPONENT_WIDTH, nameTextField.getHeight()));

		log.trace("Number of Columns: "+ nameTextField.getColumns());
		double width = nameTextField.getSize().getWidth();
		log.trace("Width: "+ width);

		//columnsComboBox.setSize((int)width, columnsComboBox.getHeight());
		//this.revalidate();
	}

	/**
	 * Creates a ListModel containing the names of the columns of the 
	 * specified table
	 * 
	 * @param dbTable The table whose columns we want to display
	 * @return A ListModel containing the columns
	 */
	private DefaultListModel createListModel(DatabaseTable dbTable)
	{
		DefaultListModel listModel = new DefaultListModel();
		
		for(TableColumn column : dbTable.getColumns())
		{
			listModel.addElement(column.getName());
		}

		listModel.addElement(NEW_COLUMN_COMMAND);

		return listModel;
	}

	/**
	 * Returns the table in reference by TableActionsPanel
	 * 
	 * @return {@link DatabaseTable}
	 */
	public DatabaseTable getTable()
	{
		return this._dbTable;
	}

	/**
	 * Returns the name of the table, which is written in the nameTextField
	 * 
	 * @return String
	 */
	public String getNameTextFieldText()
	{
		return this.nameTextField.getText();
	}
	/**
	 * This method is called from within the constructor to initialize the form.
	 * WARNING: Do NOT modify this code. The content of this method is always
	 * regenerated by the Form Editor.
	 */
	@SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        nameLabel = new javax.swing.JLabel();
        nameTextField = new javax.swing.JTextField();
        columnsLabel = new javax.swing.JLabel();
        submitButton = new javax.swing.JButton();
        submitButton.addActionListener(new EditTableListener());
        errorLabel = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        columnsList = new javax.swing.JList();

        setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());

        nameLabel.setText("Name");
        add(nameLabel, new org.netbeans.lib.awtextra.AbsoluteConstraints(30, 40, -1, -1));

        nameTextField.setToolTipText("");
        nameTextField.setMinimumSize(new java.awt.Dimension(14, 20));
        add(nameTextField, new org.netbeans.lib.awtextra.AbsoluteConstraints(70, 40, 100, -1));

        columnsLabel.setText("Columns");
        add(columnsLabel, new org.netbeans.lib.awtextra.AbsoluteConstraints(20, 100, -1, -1));

        submitButton.setText("Submit");
        add(submitButton, new org.netbeans.lib.awtextra.AbsoluteConstraints(70, 190, -1, -1));
        add(errorLabel, new org.netbeans.lib.awtextra.AbsoluteConstraints(50, 230, 124, 14));

        columnsList.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);
        jScrollPane1.setViewportView(columnsList);

        add(jScrollPane1, new org.netbeans.lib.awtextra.AbsoluteConstraints(70, 70, 100, 100));
    }// </editor-fold>//GEN-END:initComponents

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JLabel columnsLabel;
    private javax.swing.JList columnsList;
    private javax.swing.JLabel errorLabel;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JLabel nameLabel;
    private javax.swing.JTextField nameTextField;
    private javax.swing.JButton submitButton;
    // End of variables declaration//GEN-END:variables

	private DatabaseTable _dbTable;
	
	private final String NEW_COLUMN_COMMAND = "New Column...";

	/**
	 * The width of the components
	 */
	private final int COMPONENT_WIDTH = 50;

	private static Logger log = Logger.getLogger(TableActionsPanel.class);
}
